package com.douwong.student.task;

import android.os.AsyncTask;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.douwong.student.contant.NetConfig;
import com.douwong.student.interfaces.DetailNotify;
import com.douwong.student.model.FeaturesBean;
import com.douwong.student.model.HallTestData;
import com.douwong.student.model.StatusCode;
import com.douwong.student.utils.NetUtils;

import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * 备课测试详情
 * 
 * @author leihuajie
 * 
 */
public class PrepareTestDetailTask extends AsyncTask<String, Integer, Integer> {

	private int type;
	public static int REQUEST_OK = 1;
	private int ERROR=-1;
	private DetailNotify detailListener;
	private String content;

	public PrepareTestDetailTask(int type) {
		this.type = type;
	}

	@Override
	protected Integer doInBackground(String... params) {
		String response = null;
		int stateCode = REQUEST_OK;
		List<BasicNameValuePair> arrayParams = new ArrayList<BasicNameValuePair>();
		try {
			if (type == 1) {// 导读详情
				arrayParams.add(new BasicNameValuePair("introId", params[0]));
				response = NetUtils.httpGet(NetConfig.DAODU_DETAIL,
						arrayParams);
				StatusCode<FeaturesBean> sc = (StatusCode<FeaturesBean>) JSON
						.parseObject(response,
								new TypeReference<StatusCode<FeaturesBean>>() {
								});
				if (sc != null && sc.isFlag()) {
					FeaturesBean featuresBean = sc.getData();
					content = featuresBean.getIntroInfo();
				}
				Log.e("response", response + "");
			} else if (type == 2) {// 测试详情
				
				arrayParams.add(new BasicNameValuePair("examineId", params[0]));
				response = NetUtils.httpGet(NetConfig.TEST_DEATIL,
						arrayParams);
				Log.e("content", response + "");
				Log.e("params[0]", params[0] + "");
				StatusCode<HallTestData> sc = (StatusCode<HallTestData>) JSON
						.parseObject(response,
								new TypeReference<StatusCode<HallTestData>>() {
								});
				if (sc != null && sc.isFlag()) {
					content=response;
//					HallTestData hallTestData = sc.getData();
//					content = JsonUtils.serialize(hallTestData);
				}else{
					stateCode=ERROR;
				}
//				Log.e("params[0]", params[0] + "");
//				Log.e("content", content + "");
			}
		} catch (Exception e) {
			e.printStackTrace();
			stateCode=ERROR;
		}

		return stateCode;
	}

	@Override
	protected void onPostExecute(Integer result) {
		super.onPostExecute(result);
		detailListener.notify(result, content);
	}

	public void setDetailListener(DetailNotify detailListener) {
		this.detailListener = detailListener;
	}

}
